const { ccclass, property } = cc._decorator;

import n from "./frameworkManager"
import l from "./NodePool"
import s from "./EngineUtil"
import c from "./MathUtil"
import u from "./MatchGameConfig"
import p from "./MatchGameAudioMgr"
import d from "./MatchGameCenterCtrl"
import h from "./MatchGameViewCtrl"
import f from "./MatchAniCtrl"
import MatchGameConfig from "./MatchGameConfig";
import MathUtils from "./MathUtil";
import MatchGameCenterCtrl from "./MatchGameCenterCtrl";

@ccclass
class TargetAniCtrl {
    public isReady = false;
    public targetAniParent = null;
    public goldCoinParent = null;
    public targetPrefab = null;
    public smearPrefab = null;
    public mailFlySpData = null;
    public goldCoinFlySpData = null;
    public birdFlyEffectSpData = null;
    public diamondFlySpData = null;
    public matchFlySpData = null;

    constructor() {
        this.isReady = false;
        this.targetAniParent = null;
        this.goldCoinParent = null;
        this.targetPrefab = null;
        this.smearPrefab = null;
        this.mailFlySpData = null;
        this.goldCoinFlySpData = null;
        this.birdFlyEffectSpData = null;
        this.diamondFlySpData = null;
        this.matchFlySpData = null;
    }
    static _instance;
    static getInstance() {
        this._instance || (this._instance = new TargetAniCtrl());
        return this._instance;
    };
    public ready(e) {
        this.isReady = true;
        this.targetAniParent = e;
    };
    public async init(): Promise<void> {
        try {
            await this.loadTargetPrefab();
            await this.loadSmearPrefab();
            await this.loadBirdSpData();
            await this.loadGoldFlySpData();
            await this.loadMailSpData();
            await this.loadMatchSpData();
            await this.loadDiamondSpData();
        } catch (error) {
            // 错误处理
        }
    }

    public loadTargetPrefab() {
        var e = this;
        return new Promise(function (t, a) {
            cc.resources.load("prefabs/obstacleAniPrefab", cc.Prefab, function (o, i) {
                if (o) {
                    n.error("未找到障碍物特效动画预制体", o);
                    a();
                }
                e.targetPrefab = i;
                l.initPool(e.targetPrefab, 0, u.MatchAniPool.targetAniPrefab);
                t(i);
            });
        });
    };
    public loadSmearPrefab() {
        var e = this;
        return new Promise(function (t, a) {
            cc.resources.load("prefabs/smearPrefab", cc.Prefab, function (o, i) {
                if (o) {
                    n.error("未找到障碍物特效动画预制体", o);
                    a();
                }
                e.smearPrefab = i;
                l.initPool(e.smearPrefab, 0, u.MatchAniPool.smearPrefab);
                t(i);
            });
        });
    };
    public loadMailSpData() {
        var e = this;
        return new Promise(function (t, a) {
            cc.resources.load("spine/obstacleEffect/XinFeng", sp.SkeletonData, function (o, i) {
                if (o) {
                    n.error("未找到障碍物特效动画预制体", o);
                    a();
                }
                e.mailFlySpData = i;
                t(i);
            });
        });
    };
    public loadGoldFlySpData() {
        var e = this;
        return new Promise(function (t, a) {
            cc.resources.load("spine/obstacleEffect/QZ_HongBao", sp.SkeletonData, function (o, i) {
                if (o) {
                    n.error("未找到障碍物特效动画预制体", o);
                    a();
                }
                e.goldCoinFlySpData = i;
                t(i);
            });
        });
    };
    public loadMatchSpData() {
        var e = this;
        return new Promise(function (t, a) {
            cc.resources.load("spine/all/QiZi", sp.SkeletonData, function (o, i) {
                if (o) {
                    n.error("未找到障碍物特效动画预制体", o);
                    a();
                }
                e.matchFlySpData = i;
                t(i);
            });
        });
    };
    public loadBirdSpData() {
        var e = this;
        return new Promise(function (t, a) {
            cc.resources.load("spine/obstacleEffect/XiaoNiao", sp.SkeletonData, function (o, i) {
                if (o) {
                    n.error("未找到障碍物特效动画预制体", o);
                    a();
                }
                e.birdFlyEffectSpData = i;
                t(i);
            });
        });
    };
    public loadDiamondSpData() {
        var e = this;
        return new Promise(function (t, a) {
            cc.resources.load("spine/obstacleEffect/Y_A_YanShi-BaoShi", sp.SkeletonData, function (o, i) {
                if (o) {
                    n.error("未找到障碍物特效动画预制体", o);
                    a();
                }
                e.diamondFlySpData = i;
                t(i);
            });
        });
    };
    public createObstacleEffectItem(e, t, a?) {
        var o = l.getNode(u.MatchAniPool.targetAniPrefab);
        o.parent = this.targetAniParent;
        o.setPosition(e);
        var i = o.getComponent(sp.Skeleton);
        o.angle = 0;
        i.skeletonData = t;
        i.premultipliedAlpha = false;
        i.clearTracks();
        i.setCompleteListener(function () {
            i.setCompleteListener(null);
            i.skeletonData = null;
            l.putNode(u.MatchAniPool.targetAniPrefab, o);
            a && a();
        });
        return i;
    };
    public playDiamondFlyAni(e, t, a) {
        var o = l.getNode(u.MatchAniPool.targetAniPrefab);
        o.parent = this.targetAniParent;
        o.active = true;
        var i = o.getComponent(sp.Skeleton);
        i.skeletonData = this.diamondFlySpData;
        i.setAnimation(0, "idle_4", false);
        i.setCompleteListener(function () { });
        var r = s.getNodeAToNodeBPoint(d.node, this.targetAniParent), n = cc.v3(e.x + r.x + MatchGameConfig.ITEM_WIDTH / 2, e.y + r.y - MatchGameConfig.ITEM_HEIGHT / 2, 0);
        o.setPosition(cc.v3(n));
        var c = cc.v2(s.getNodeAToNodeBPoint(t, i.node)), p = l.getNode(u.MatchAniPool.smearPrefab);
        p.setParent(this.targetAniParent);
        p.setPosition(cc.v3(n.x, e.y, 0));
        var h = s.getRandomNumber(6, 20) / 10;
        cc.tween(i.node).delay(.2).to(h, {
            position: cc.v3(c.x, c.y, 0)
        }, {
            easing: "backOut"
        }).call(function () {
            i.setCompleteListener(null);
            i.skeletonData = null;
            l.putNode(u.MatchAniPool.targetAniPrefab, i.node);
            a && a();
        }).start();
        cc.tween(p).delay(.2).to(h, {
            position: cc.v3(c.x, c.y, 0)
        }, {
            easing: "backOut"
        }).call(function () {
            l.putNode(u.MatchAniPool.smearPrefab, p);
        }).start();
    };
    public playMailMoveAni(e, t, a) {
        var o = l.getNode(u.MatchAniPool.targetAniPrefab);
        o.parent = this.targetAniParent;
        var i = o.getComponent(sp.Skeleton);
        i.skeletonData = this.mailFlySpData;
        i.setAnimation(0, "explode_1", false);
        var r = s.getNodeAToNodeBPoint(d.node, this.targetAniParent);
        i.node.setPosition(cc.v3(e.x + r.x + MatchGameConfig.ITEM_WIDTH / 2, e.y + r.y - MatchGameConfig.ITEM_HEIGHT / 2));
        var n = s.getNodeAToNodeBPoint(t, o), p = cc.v3(n.x, n.y - 35), h = cc.v3(p.x, p.y - 100), f = MathUtils.getDistance(i.node.position, h) / 1e3;
        i.setCompleteListener(function () {
            cc.tween(i.node).to(f, {
                position: h
            }, {
                easing: "backOut"
            }).delay(.2).to(.1, {
                scale: .3,
                position: p
            }).delay(.3).call(function () {
                i.setCompleteListener(null);
                i.skeletonData = null;
                l.putNode(u.MatchAniPool.targetAniPrefab, i.node);
                a && a();
            }).start();
        });
    };
    public playGoldCoinMoveToTargetAni(e, t, a) {
        var o = l.getNode(u.MatchAniPool.targetAniPrefab);
        o.parent = this.targetAniParent;
        var i = s.getNodeAToNodeBPoint(MatchGameCenterCtrl._instance.node, this.targetAniParent);
        o.setPosition(cc.v3(e.x + i.x, e.y + i.y));
        var r = o.getComponent(sp.Skeleton);
        o.angle = 0;
        r.skeletonData = this.goldCoinFlySpData;
        r.premultipliedAlpha = false;
        r.clearTracks();
        r.setAnimation(0, "idle_1", true);
        var n = cc.v3(s.getNodeAToNodeBPoint(t, r.node)), p = cc.v3(n.x, n.y), h = MathUtils.getDistance(r.node.position, p) / 700;
        cc.tween(r.node).delay(.1).to(h, {
            position: p
        }).delay(.2).call(function () {
            r.setCompleteListener(null);
            r.skeletonData = null;
            l.putNode(u.MatchAniPool.targetAniPrefab, r.node);
            a && a();
        }).start();
    };
    public playTargetCoinToRedPackAni(e, t, a) {
        var o = l.getNode(u.MatchAniPool.targetAniPrefab);
        o.parent = this.targetAniParent;
        var i = o.getComponent(sp.Skeleton);
        o.angle = 0;
        i.skeletonData = this.goldCoinFlySpData;
        i.premultipliedAlpha = false;
        i.clearTracks();
        i.setAnimation(0, "idle_3", false);
        i.addAnimation(0, "idle_1", true);
        o.scale = 1.2;
        var r = cc.v3(s.getNodeAToNodeBPoint(e, i.node));
        o.setPosition(cc.v3(r));
        var n = cc.v2(s.getNodeAToNodeBPoint(t, i.node)), d = MathUtils.CalculationMidPoint(cc.v2(r), n, s.getRandomNumber(2, 8) / 10), h = cc.v2(d.x + s.getRandomNumber(100, 1e3), d.y), f = l.getNode(u.MatchAniPool.smearPrefab);
        f.setParent(this.targetAniParent);
        f.setPosition(cc.v3(r));
        var g = s.getRandomNumber(6, 20) / 10;
        cc.tween(o).delay(.2).bezierTo(g, cc.v2(r), h, n).delay(.1).call(function () {
            i.setCompleteListener(null);
            i.skeletonData = null;
            l.putNode(u.MatchAniPool.targetAniPrefab, i.node);
            a && a();
            p.playCoinTargetToRedPackAudio();
        }).start();
        cc.tween(f).delay(.2).bezierTo(g, cc.v2(r), h, n).call(function () {
            l.putNode(u.MatchAniPool.smearPrefab, f);
        }).start();
    };
    public playFlyBoxToRedPackAni(e, t, a) {
        var o = l.getNode(u.MatchAniPool.targetAniPrefab);
        o.parent = this.targetAniParent;
        var i = o.getComponent(sp.Skeleton);
        o.angle = 0;
        i.skeletonData = this.goldCoinFlySpData;
        i.premultipliedAlpha = false;
        i.clearTracks();
        i.setAnimation(0, "idle_3", false);
        i.addAnimation(0, "idle_1", true);
        o.scale = 1.2;
        var r = cc.v3(s.getNodeAToNodeBPoint(e, i.node));
        o.setPosition(cc.v3(r));
        var n = cc.v2(s.getNodeAToNodeBPoint(t, i.node)), d = MathUtils.CalculationMidPoint(cc.v2(r), n, s.getRandomNumber(2, 8) / 10), h = cc.v2(d.x + s.getRandomNumber(100, 1e3), d.y), f = l.getNode(u.MatchAniPool.smearPrefab);
        f.setParent(this.targetAniParent);
        f.setPosition(cc.v3(r));
        var g = s.getRandomNumber(6, 20) / 10;
        cc.tween(o).delay(.2).bezierTo(g, cc.v2(r), h, n).delay(.1).call(function () {
            i.setCompleteListener(null);
            i.skeletonData = null;
            l.putNode(u.MatchAniPool.targetAniPrefab, i.node);
            a && a();
            p.playCoinTargetToRedPackAudio();
        }).start();
        cc.tween(f).delay(.2).bezierTo(g, cc.v2(r), h, n).call(function () {
            l.putNode(u.MatchAniPool.smearPrefab, f);
        }).start();
    };
    public playGoldCoinMoveToRedPackAni2(e, t) {
        var a = l.getNode(u.MatchAniPool.smearPrefab);
        a.setParent(this.targetAniParent);
        a.active = false;
        var o = l.getNode(u.MatchAniPool.targetAniPrefab);
        o.parent = this.targetAniParent;
        var i = s.getNodeAToNodeBPoint(d._instance.node, this.targetAniParent);
        o.scale = 1;
        o.setPosition(cc.v3(e.x + i.x, e.y + i.y));
        var r = MathUtils.getPosOnCircle(cc.v2(e.x + i.x, e.y + i.y), 100, 90), n = o.getComponent(sp.Skeleton);
        o.angle = 0;
        n.skeletonData = this.goldCoinFlySpData;
        n.premultipliedAlpha = false;
        o.scale = 1;
        n.clearTracks();
        n.setAnimation(0, "idle_3", false);
        n.addAnimation(0, "idle_2", true);
        var p = cc.v3(s.getNodeAToNodeBPoint(h.instance.ui.sp_redpack, o)), f = cc.v3(p.x, p.y + 50), g = MathUtils.getDistance(r, f) / 3e3;
        cc.tween(n.node).to(.3, {
            y: o.position.y + 50,
            scale: 1.2
        }, {
            easing: "backOut"
        }).delay(.3).to(g, {
            position: f,
            scale: 1
        }).call(function () {
            n.setCompleteListener(null);
            n.skeletonData = null;
            l.putNode(u.MatchAniPool.targetAniPrefab, n.node);
            t && t();
        }).start();
        cc.tween(a).to(.3, {
            y: o.position.y + 50,
            scale: 1.2
        }, {
            easing: "backOut"
        }).delay(.4).call(function () {
            a.position = cc.v3(e.x + i.x, e.y + i.y);
            a.active = true;
        }).to(g + .1, {
            position: f,
            scale: 1
        }).call(function () {
            l.putNode(u.MatchAniPool.smearPrefab, a);
        }).start();
    };
    public playGoldCoinMoveToRedPackAni1(e, t) {
        var a = l.getNode(u.MatchAniPool.targetAniPrefab);
        a.parent = this.targetAniParent;
        var o = s.getNodeAToNodeBPoint(d.node, this.targetAniParent);
        a.scale = 1;
        a.setPosition(cc.v3(e.x + o.x, e.y + o.y));
        var i = MathUtils.getPosOnCircle(cc.v2(e.x + o.x, e.y + o.y), 100, 90), r = a.getComponent(sp.Skeleton);
        a.angle = 0;
        r.skeletonData = this.goldCoinFlySpData;
        r.premultipliedAlpha = false;
        a.scale = 1;
        r.clearTracks();
        r.setAnimation(0, "idle_3", false);
        r.addAnimation(0, "idle_1", true);
        var n = cc.v3(s.getNodeAToNodeBPoint(h.instance.ui.sp_redpack, a)), p = cc.v3(n.x, n.y + 100), f = MathUtils.getDistance(i, p) / 1e3;
        cc.tween(r.node).to(.3, {
            position: i,
            scale: 1.2
        }, {
            easing: "backInOut"
        }).delay(.3).to(f + .1, {
            position: n,
            scale: .8
        }).delay(.3).call(function () {
            r.setCompleteListener(null);
            r.skeletonData = null;
            l.putNode(u.MatchAniPool.targetAniPrefab, r.node);
            t && t();
        }).start();
    };
    public playBirdFlyAni(e, t, a) {
        var o = this.createObstacleEffectItem(e, this.birdFlyEffectSpData), i = s.getNodeAToNodeBPoint(d.node, this.targetAniParent);
        o.node.setPosition(cc.v3(e.x + i.x, e.y + i.y));
        o.setAnimation(0, "flight", false);
        o.setAnimation(1, "flying", true);
        var r = cc.v3(s.getNodeAToNodeBPoint(t, o.node)), n = cc.v3(r.x, r.y), p = MathUtils.getDistance(o.node.position, n) / 1e3;
        o.setCompleteListener(function () {
            if ("flight" == o.animation) {
                o.setCompleteListener(null);
                cc.tween(o.node).to(p, {
                    position: n
                }).delay(.2).call(function () {
                    o.setCompleteListener(null);
                    o.skeletonData = null;
                    l.putNode(u.MatchAniPool.targetAniPrefab, o.node);
                    a && a();
                }).start();
            }
        });
    };
    public playMatchFlyAni(e, t, a, o) {
        try {
            if (cc.isValid(t)) {
                var i = this.createObstacleEffectItem(e, this.matchFlySpData), r = s.getNodeAToNodeBPoint(d.node, this.targetAniParent);
                i.node.setPosition(cc.v3(e.x + r.x, e.y + r.y));
                i.setCompleteListener(null);
                i.setAnimation(0, "idle_1", false);
                i.setSkin(f.setMatchAniSkin(a));
                var p = cc.v3(s.getNodeAToNodeBPoint(t, i.node)), h = cc.v3(p.x, p.y), g = MathUtils.getDistance(i.node.position, h) / 1e3;
                cc.tween(i.node).to(g, {
                    position: h
                }).delay(.2).to(.1, {
                    scale: .3,
                    position: p
                }).call(function () {
                    o && o();
                    if (i) {
                        i.setCompleteListener(null);
                        i.skeletonData = null;
                        l.putNode(u.MatchAniPool.targetAniPrefab, i.node);
                    }
                }).start();
                return g + .2 + .1;
            }
        } catch (e) {
            n.error("playMatchFlyAni==error==", e);
        }
    };
    public getV3Pos(e, t) {
        return cc.v3(e * MatchGameConfig.ITEM_WIDTH + MatchGameConfig.ITEM_WIDTH / 2, -t * MatchGameConfig.ITEM_HEIGHT - MatchGameConfig.ITEM_HEIGHT / 2);
    };
}
export default TargetAniCtrl.getInstance();